/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.ufmg.dees.insane.commons.dialogs.DecimalField;
import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.model.disc.element.FrameElement;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.model.disc.pointforce.PointForce;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * A class representing creation dialog on screen.
 * For example:
 * <pre>
 *    ElmConcForcesDialog dgl = new ElmConcForcesDialog(parent);
 *    dgl.pack();
 * </pre>
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since April 2004
 */
public final class ElmConcForcesDialog extends SimpleDialog {
	
	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
	private NumberFormat numberFormat;
	private NumberFormat numberFormat2;
	private CardLayout cardManager = new CardLayout();
	private FemModel fmodel;
	
	private String anlType;
	
	private JPanel concButPane = new JPanel();
	private boolean concNewPressed = false;
	private String[] concLabelList;
	private JComboBox concListCombo = new JComboBox();
	private JButton concNewButton = new JButton("New");
	private JButton concModButton = new JButton("Modify");
	private JButton concOkButton = new JButton("OK");
	private JButton concCancButton = new JButton("Cancel");
	private JTextField concLabelField = new JTextField();
	private JLabel concLLabel = new JLabel();
	private DecimalField concAField;
	private DecimalField concFxField;
	private DecimalField concFyField;
	private DecimalField concFzField;
	private DecimalField concMxField;
	private DecimalField concMyField;
	private DecimalField concMzField;
	public ArrayList<PointForce> concForces = new ArrayList<PointForce>();
	
//*****************************************************************************
	
	/**
	 * Constructor method, calling super with the especific title.
	 *
	 * @param parent The parent of the class.
	 * @see gui.InsaneInternalInterface
	 */
	public ElmConcForcesDialog(InsaneInternalInterface parent) {
		this.parent = parent;
		fmodel = parent.getModel().getDiscreteModel();
		
		concNewButton.setText(parent.getResourceBundle().getString("gNewButton"));
		concModButton.setText(parent.getResourceBundle().getString("gModifyButton"));
		concOkButton.setText(parent.getResourceBundle().getString("gOkButton"));
		concCancButton.setText(parent.getResourceBundle().getString("gCancelButton"));
		
		setUpFormat();
		setUpFields();
		
		setUpConcList();
		setUpConcCombo();
		
		//Sets up the concentrated forces panel
		concListCombo = new JComboBox(concLabelList);
		
		JPanel concForcesPane = new JPanel();
		concForcesPane.setPreferredSize(new Dimension(360,300));
		concForcesPane.setSize(new Dimension(360,300));
		concForcesPane.setLayout(new BoxLayout(concForcesPane, BoxLayout.Y_AXIS));
		
		concLabelField.setMaximumSize(new Dimension(100,26));
		concListCombo.setMaximumSize(new Dimension(100,26));
		concNewButton.setMaximumSize(new Dimension(100,26));
		concModButton.setMaximumSize(new Dimension(100,26));
		concOkButton.setMaximumSize(new Dimension(100,26));
		concCancButton.setMaximumSize(new Dimension(100,26));
		
		concLabelField.setPreferredSize(new Dimension(100,26));
		concListCombo.setPreferredSize(new Dimension(100,26));
		concNewButton.setPreferredSize(new Dimension(100,26));
		concModButton.setPreferredSize(new Dimension(100,26));
		concOkButton.setPreferredSize(new Dimension(100,26));
		concCancButton.setPreferredSize(new Dimension(100,26));
		
		concListCombo.setMaximumRowCount(5);
		
		concListCombo.addItemListener(new ConcComboListener());
		concNewButton.addActionListener(new ConcNewListener());
		concModButton.addActionListener(new ConcModListener());
		concOkButton.addActionListener(new ConcOkListener());
		concCancButton.addActionListener(new ConcCancListener());
		
		concAField.setEditable(false);
		concFxField.setEditable(false);
		concFyField.setEditable(false);
		concFzField.setEditable(false);
		concMxField.setEditable(false);
		concMyField.setEditable(false);
		concMzField.setEditable(false);
		
		String imgName = "br/ufmg/dees/insane/images/";
		imgName += parent.getResourceBundle().getString("imgConcForces100");
		
		JPanel concPicPanel = new JPanel();
		concPicPanel.setMaximumSize(new Dimension(500,100));
		concPicPanel.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource(imgName)) ));
		concForcesPane.add(concPicPanel);
		
		concButPane.setLayout(cardManager);
			
		JPanel concBut1Pane = new JPanel();
		concBut1Pane.setLayout(new GridLayout(1,2,2,2));
        concBut1Pane.setMaximumSize(new Dimension(500,120));
		JPanel concBut1aPane = new JPanel();
		concBut1aPane.setLayout(new GridLayout(1,2,2,2));
		concBut1aPane.add(concNewButton);
		concBut1aPane.add(concModButton);
		concBut1Pane.add(concListCombo);
		concBut1Pane.add(concBut1aPane);
		
		JPanel concBut2Pane = new JPanel();
		concBut2Pane.setLayout(new GridLayout(1,2,2,2));
        concBut2Pane.setMaximumSize(new Dimension(500,120));
		concBut2Pane.add(concLabelField);
		JPanel concBut2aPane = new JPanel();
		concBut2aPane.setLayout(new GridLayout(1,2,2,2));
		concBut2aPane.add(concOkButton);
		concBut2aPane.add(concCancButton);
		concBut2Pane.add(concBut2aPane);
		
		concButPane.add(concBut1Pane, "concCombo");
		concButPane.add(concBut2Pane, "concLabel");
		
		concForcesPane.add(concButPane);
		
		JPanel concDistPane = new JPanel();
		concDistPane.setMaximumSize(new Dimension(500,80));
		concDistPane.setBorder(BorderFactory.createCompoundBorder(
        	BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dEConcForDialog01")),
			BorderFactory.createEmptyBorder(2,2,2,2)));
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		concDistPane.setLayout(gridbag);
		c.fill = GridBagConstraints.HORIZONTAL;
		JPanel concAPane = new JPanel();
		concAPane.add(new JLabel("A:"));
		concAPane.add(concAField);
		c.gridwidth = 1;
		c.gridx = 0;
		c.gridy = 0;
		c.anchor = GridBagConstraints.WEST;
		gridbag.setConstraints(concAPane, c);
		concDistPane.add(concAPane);
		JPanel concLPane = new JPanel();
		concLPane.add(concLLabel);
		c.gridwidth = 2;
		c.gridx = 1;
		c.gridy = 0;
		c.weightx = 1;
		c.anchor = GridBagConstraints.LINE_END;
		gridbag.setConstraints(concLPane, c);
		concDistPane.add(concLPane);
		concForcesPane.add(concDistPane);
		
		JPanel concValuesPane = new JPanel();
		concValuesPane.setLayout(new GridLayout(2,3,2,2));
        concValuesPane.setMaximumSize(new Dimension(500,300));
		concValuesPane.setBorder(BorderFactory.createCompoundBorder(
        	BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dEConcForDialog02")),
			BorderFactory.createEmptyBorder(2,2,2,2)));
		
		JPanel concFxPanel = new JPanel();
		concFxPanel.add(new JLabel("Fx:"));
		concFxPanel.add(concFxField);
		concValuesPane.add(concFxPanel);
		
		JPanel concFyPanel = new JPanel();
		concFyPanel.add(new JLabel("Fy:"));
		concFyPanel.add(concFyField);
		concValuesPane.add(concFyPanel);
		
		JPanel concFzPanel = new JPanel();
		concFzPanel.add(new JLabel("Fz:"));
		concFzPanel.add(concFzField);
		concValuesPane.add(concFzPanel);
		
		JPanel concMxPanel = new JPanel();
		concMxPanel.add(new JLabel("Mx:"));
		concMxPanel.add(concMxField);
		concValuesPane.add(concMxPanel);
		
		JPanel concMyPanel = new JPanel();
		concMyPanel.add(new JLabel("My:"));
		concMyPanel.add(concMyField);
		concValuesPane.add(concMyPanel);
		
		JPanel concMzPanel = new JPanel();
		concMzPanel.add(new JLabel("Mz:"));
		concMzPanel.add(concMzField);
		concValuesPane.add(concMzPanel);
		
		concForcesPane.add(concValuesPane);
		
		//Perhaps it will be removed
		JPanel concEmptyPanel = new JPanel();
		concEmptyPanel.setMinimumSize(new Dimension(300, 50));
		concEmptyPanel.setMaximumSize(new Dimension(300, 50));
		concEmptyPanel.setPreferredSize(new Dimension(300, 50));
		concForcesPane.add(concEmptyPanel);
		
		setPane(concForcesPane);
	
    }
	
//*****************************************************************************
	
    public int showDialog() {
    	return super.showDialog(parent, parent.getResourceBundle().getString("dEConcForDialog03"));
    }
	
//*****************************************************************************
	
	/**
	 * This method is executed on OK button click.
	 */
	public void approvedPerform() {}
	
//*****************************************************************************
	
	/**
	 * Sets up the number formater
	 */
	private void setUpFormat() {
		numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
		numberFormat.setMinimumFractionDigits(parent.getModelState().getPrecision());
		numberFormat.setMaximumFractionDigits(8);
		numberFormat2 = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
		numberFormat2.setMinimumFractionDigits(parent.getModelState().getPrecision());
		numberFormat2.setMaximumFractionDigits(parent.getModelState().getPrecision());
	}
	
//*****************************************************************************
	
	/**
	 * Sets up fields
	 */
	private void setUpFields() {
		
		if (this.parent.getController().getSelection().size() == 1 ) {
			
			FrameElement elm = (FrameElement)this.parent.getController().getSelection().get(0);
			
			//Setting concentrated forces fields
			String txt = parent.getResourceBundle().getString("dEConcForDialog04") + " = ";
			String txt2 = numberFormat2.format(elm.getLength());
			txt = txt + txt2;
			concLLabel.setText(txt);
			
			//Copying actual ElmPointForcesList
			ListIterator pfs = elm.getElmPointForcesList().listIterator();
			while (pfs.hasNext()) {
				
				PointForce pf1 = (PointForce)pfs.next();
				PointForce pf2 = new PointForce();
				
				pf2.getCoord().x = pf1.getCoord().x;
				pf2.setForce(0, pf1.getForce(0));
				pf2.setForce(1, pf1.getForce(1));
				pf2.setForce(2, pf1.getForce(2));
				pf2.setForce(3, pf1.getForce(3));
				pf2.setForce(4, pf1.getForce(4));
				pf2.setForce(5, pf1.getForce(5));
				
				concForces.add(pf2);
			}
			
			if (concForces.size() > 0) {
				PointForce force = (PointForce)concForces.get(0);
				concAField = new DecimalField(force.getCoord().x, 7, numberFormat);
				concFxField = new DecimalField(force.getForce(0), 7, numberFormat);
				concFyField = new DecimalField(force.getForce(1), 7, numberFormat);
				concFzField = new DecimalField(force.getForce(2), 7, numberFormat);
				concMxField = new DecimalField(force.getForce(3), 7, numberFormat);
				concMyField = new DecimalField(force.getForce(4), 7, numberFormat);
				concMzField = new DecimalField(force.getForce(5), 7, numberFormat);
			} else {
				concAField = new DecimalField(0, 7, numberFormat);
				concFxField = new DecimalField(0, 7, numberFormat);
				concFyField = new DecimalField(0, 7, numberFormat);
				concFzField = new DecimalField(0, 7, numberFormat);
				concMxField = new DecimalField(0, 7, numberFormat);
				concMyField = new DecimalField(0, 7, numberFormat);
				concMzField = new DecimalField(0, 7, numberFormat);
				concAField.setText("");
				concFxField.setText("");
				concFyField.setText("");
				concFzField.setText("");
				concMxField.setText("");
				concMyField.setText("");
				concMzField.setText("");
				concModButton.setEnabled(false);
			}
			
		} else {
			
			//Setting concentrated forces fields
			concAField = new DecimalField(0, 7, numberFormat);
			concFxField = new DecimalField(0, 7, numberFormat);
			concFyField = new DecimalField(0, 7, numberFormat);
			concFzField = new DecimalField(0, 7, numberFormat);
			concMxField = new DecimalField(0, 7, numberFormat);
			concMyField = new DecimalField(0, 7, numberFormat);
			concMzField = new DecimalField(0, 7, numberFormat);
			String txt = parent.getResourceBundle().getString("dEConcForDialog04") + " = N/A";
			concLLabel.setText(txt);
			concAField.setText("");
			concFxField.setText("");
			concFyField.setText("");
			concFzField.setText("");
			concMxField.setText("");
			concMyField.setText("");
			concMzField.setText("");
			concModButton.setEnabled(false);
			
		}
		
		//Enabling or disabling fields according to analysis model type
		if (this.parent.getController().getSelection().size() == 1)
			anlType = ((FrameElement)this.parent.getController().getSelection().get(0)).getAnalysisModel().getType();
		else anlType = fmodel.getGlobalAnalysisModel().getType();
		
		if (anlType.equals("Beam")) {
			
			concFxField.setEnabled(false);
			concFyField.setEnabled(true);
			concFzField.setEnabled(false);
			concMxField.setEnabled(false);
			concMyField.setEnabled(false);
			concMzField.setEnabled(true);
			
		} else if (anlType.equals("PlaneFrame")) {
			
			concFxField.setEnabled(true);
			concFyField.setEnabled(true);
			concFzField.setEnabled(false);
			concMxField.setEnabled(false);
			concMyField.setEnabled(false);
			concMzField.setEnabled(true);
			
		} else if (anlType.equals("PlaneTruss")) {
			
			concFxField.setEnabled(true);
			concFyField.setEnabled(true);
			concFzField.setEnabled(false);
			concMxField.setEnabled(false);
			concMyField.setEnabled(false);
			concMzField.setEnabled(false);
		
		} else if (anlType.equals("Grid")) {
			
			concFxField.setEnabled(false);
			concFyField.setEnabled(true);
			concFzField.setEnabled(false);
			concMxField.setEnabled(true);
			concMyField.setEnabled(false);
			concMzField.setEnabled(true);
		
		}
	}
	
//*****************************************************************************
	
	public void setUpConcList() {
		
int num = concForces.size();
		
		if (num > 0) {
			concLabelList = new String[num];
			for (int i=0; i<num; i++)
				concLabelList[i] = (parent.getResourceBundle().getString("dEConcForDialog05") + " " + (i+1));
		} else {
			concLabelList = new String[1];
			concLabelList[0] = "----- " + parent.getResourceBundle().getString("dEConcForDialog06") + " -----";
		}
		
	}
	
//*****************************************************************************
	
	public void setUpConcCombo() {
		
		concListCombo.removeAllItems();
		for (int i=0; i<concLabelList.length; i++)
			concListCombo.addItem(concLabelList[i]);
		
	}
	
//*****************************************************************************
	
	/** Private inner class to handle button events. */
	private class ConcComboListener implements ItemListener {
		public void itemStateChanged(ItemEvent evt) {
			
			int index = concListCombo.getSelectedIndex();
			int listSize = concForces.size();
			
			if (listSize>0 && index>=0) {
				
				PointForce force = (PointForce)concForces.get(index);
				
				concAField.setValue(force.getCoord().x);
				concFxField.setValue(force.getForce(0));
				concFyField.setValue(force.getForce(1));
				concFzField.setValue(force.getForce(2));
				concMxField.setValue(force.getForce(3));
				concMyField.setValue(force.getForce(4));
				concMzField.setValue(force.getForce(5));
				
				concModButton.setEnabled(true);
			
			} else {
				
				concAField.setText(" ");
				concFxField.setText(" ");
				concFyField.setText(" ");
				concFzField.setText(" ");
				concMxField.setText(" ");
				concMyField.setText(" ");
				concMzField.setText(" ");
				
				concModButton.setEnabled(false);
				
			}
			
		}
	}
	
//*****************************************************************************
	
	/** Private inner class to handle button events. */
	private class ConcNewListener implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			
			cardManager.show(concButPane, "concLabel");
			setButtonsEnabled(false);
			
			concAField.setEditable(true);
			concFxField.setEditable(true);
			concFyField.setEditable(true);
			concFzField.setEditable(true);
			concMxField.setEditable(true);
			concMyField.setEditable(true);
			concMzField.setEditable(true);
			
			int index = concForces.size()+1;
			
			concLabelField.setText(parent.getResourceBundle().getString("dEConcForDialog05") + " " + index);
			concAField.setValue(0);
			concFxField.setValue(0);
			concFyField.setValue(0);
			concFzField.setValue(0);
			concMxField.setValue(0);
			concMyField.setValue(0);
			concMzField.setValue(0);
			
			concNewPressed = true;
			
		}
	}
	
//*****************************************************************************
	
	/** Private inner class to handle button events. */
	private class ConcModListener implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			
			cardManager.show(concButPane, "concLabel");
			setButtonsEnabled(false);
			
			concNewPressed = false;
			
			int index = concListCombo.getSelectedIndex();
			
			concLabelField.setText(parent.getResourceBundle().getString("dEConcForDialog05") + " " + (index+1));
			
			concAField.setEditable(true);
			concFxField.setEditable(true);
			concFyField.setEditable(true);
			concFzField.setEditable(true);
			concMxField.setEditable(true);
			concMyField.setEditable(true);
			concMzField.setEditable(true);
			
			if (anlType.equals("Beam")) {
				concFxField.setEnabled(false);
				concFyField.setEnabled(true);
				concFzField.setEnabled(false);
				concMxField.setEnabled(false);
				concMyField.setEnabled(false);
				concMzField.setEnabled(true);
			} else if (anlType.equals("PlaneFrame")) {
				concFxField.setEnabled(true);
				concFyField.setEnabled(true);
				concFzField.setEnabled(false);
				concMxField.setEnabled(false);
				concMyField.setEnabled(false);
				concMzField.setEnabled(true);
			} else if (anlType.equals("PlaneTruss")) {
				concFxField.setEnabled(true);
				concFyField.setEnabled(true);
				concFzField.setEnabled(false);
				concMxField.setEnabled(false);
				concMyField.setEnabled(false);
				concMzField.setEnabled(false);
			}
			
		}
	}
	
//*****************************************************************************
	
	/** Private inner class to handle button events. */
	private class ConcOkListener implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			
			boolean dataOk = true;
			int currentIndex = concListCombo.getSelectedIndex();
			
			//Checking if the distance a is higher than the length of the element
			//It must be less than the length.
			if (parent.getController().getSelection().size() == 1 ) {
				FrameElement elm = (FrameElement)parent.getController().getSelection().get(0);
				double l = elm.getLength();
				double a = concAField.getValue();
				if (a>l){
					JOptionPane.showMessageDialog(parent,
						parent.getResourceBundle().getString("dEConcForDialog07"),
						parent.getResourceBundle().getString("cmError"), JOptionPane.ERROR_MESSAGE);
					concAField.setValue(l);
					dataOk = false;
				}
			}
			
			if (dataOk) {
				
				cardManager.show(concButPane, "concCombo");
				setButtonsEnabled(true);
				
				concAField.setEditable(false);
				concFxField.setEditable(false);
				concFyField.setEditable(false);
				concFzField.setEditable(false);
				concMxField.setEditable(false);
				concMyField.setEditable(false);
				concMzField.setEditable(false);
				
				double a = concAField.getValue();
				double fx = concFxField.getValue();
				double fy = concFyField.getValue();
				double fz = concFzField.getValue();
				double mx = concMxField.getValue();
				double my = concMyField.getValue();
				double mz = concMzField.getValue();
				
				PointForce force = new PointForce();
				
				if (concNewPressed) {
					
					force.setForce(0, fx);
					force.setForce(1, fy);
					force.setForce(2, fz);
					force.setForce(3, mx);
					force.setForce(4, my);
					force.setForce(5, mz);
					force.setCoord(a, 0, 0);
					
					concForces.add(force);
					
					setUpConcList();
					setUpConcCombo();
					concListCombo.setSelectedIndex(concForces.size()-1);
					
					concNewPressed = false;
					
				} else {
					
					force = (PointForce)concForces.get(currentIndex);
					force.setForce(0, fx);
					force.setForce(1, fy);
					force.setForce(2, fz);
					force.setForce(3, mx);
					force.setForce(4, my);
					force.setForce(5, mz);
					force.setCoord(a, 0, 0);
					
					concListCombo.setSelectedIndex(currentIndex);
				}
				
			}
		}
	}
	
//*****************************************************************************
	
	/** Private inner class to handle button events. */
	private class ConcCancListener implements ActionListener {
		public void actionPerformed(ActionEvent evt) {
			
			cardManager.show(concButPane, "concCombo");
			setButtonsEnabled(true);
			
			concAField.setEditable(false);
			concFxField.setEditable(false);
			concFyField.setEditable(false);
			concFzField.setEditable(false);
			concMxField.setEditable(false);
			concMyField.setEditable(false);
			concMzField.setEditable(false);
			
			int listSize = concForces.size();
			int index = concListCombo.getSelectedIndex();
			
			if (listSize==0) {
				
				concAField.setText("");
				concFxField.setText("");
				concFyField.setText("");
				concFzField.setText("");
				concMxField.setText("");
				concMyField.setText("");
				concMzField.setText("");
				
			} else {
				PointForce force = (PointForce)concForces.get(index);
				
				concAField.setValue(force.getCoord().x);
				concFxField.setValue(force.getForce(0));
				concFyField.setValue(force.getForce(1));
				concFzField.setValue(force.getForce(2));
				concMxField.setValue(force.getForce(3));
				concMyField.setValue(force.getForce(4));
				concMzField.setValue(force.getForce(5));
				
			}
			
			concNewPressed = false;
			
		}
	}
	
//*****************************************************************************
}